package com.coca1cole.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.coca1cole.auth.application.convert.AuthRolePermissionDTOConverter;
import com.coca1cole.auth.application.dto.AuthRolePermissionDTO;
import com.coca1cole.auth.domain.entity.AuthRolePermissionBO;
import com.coca1cole.auth.domain.service.AuthRolePermissionDomainService;
import com.coca1cole.auth.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * controller
 *
 * @author coca1cole
 * @since 2024-01-22 18:31:00
 */
@RestController
@RequestMapping("/rolePermission/")
@Slf4j
public class AuthRolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限关联关系
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()), "权限关联不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色不能为空!");
            AuthRolePermissionBO rolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            return Result.ok(authRolePermissionDomainService.add(rolePermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色权限失败");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRolePermissionController.update.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getId(), "不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getPermissionId(), "不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            return Result.ok(authRolePermissionDomainService.update(authRolePermissionBO));
        } catch (Exception e) {
            log.error("AuthRolePermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新信息失败");
        }

    }

    /**
     * 删除
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRolePermissionController.delete.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getId(), "不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getPermissionId(), "不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTOToBO(authRolePermissionDTO);
            return Result.ok(authRolePermissionDomainService.delete(authRolePermissionBO));
        } catch (Exception e) {
            log.error("AuthRolePermissionController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除信息失败");
        }

    }

}
